
import scala.collection.Seq;
import scala.collection.mutable.ListBuffer;

import java.util.ArrayList;
import java.util.List;

public class ScalaUtil {

    private ScalaUtil() {
        // Prevent instantiation
    }

    public static <T> T[] seq2Array(Seq<?> seq, T[] typeArray) {
        if (seq == null) {
            throw new NullPointerException("seq is null");
        }
        if (seq.isEmpty()) {
            return typeArray;
        }
        return scala.jdk.CollectionConverters
                .SeqHasAsJava(seq)
                .asJava()
                .toArray(typeArray);
    }

    public static <T> List<T> seq2List(Seq<T> seq) {
        if (seq == null) {
            throw new NullPointerException("seq is null");
        }
        if (seq.isEmpty()) {
            return new ArrayList<>();
        }
        return scala.jdk.CollectionConverters.SeqHasAsJava(seq).asJava();
    }

    public static <T> Seq<T> array2Seq(T[] array) {
        if (array == null) {
            throw new NullPointerException("array is null");
        }
        ListBuffer<T> listBuffer = new ListBuffer<>();
        for (T t : array) {
            listBuffer.append(t);
        }
        return listBuffer.toSeq();
    }
    public static <T> Seq<T> list2Seq(List<T> list) {
        if (list == null) {
            throw new NullPointerException("list is null");
        }
        ListBuffer<T> listBuffer = new ListBuffer<>();
        for (T t : list) {
            listBuffer.append(t);
        }
        return listBuffer.toSeq();
    }
}
